﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace whereIsTheDonutGame
{

    public class MovableBox
    {
        public donutGame game;
        public Model Model { get; set; }
        public Vector3 Position { get; set; }

        public BoundingBox boundingBox { get; set; }

        public MovableBox(donutGame game)
        {
            this.game = game;
            Model = null;
            boundingBox = new BoundingBox();
        }
        public void LoadContent(ContentManager content)
        {
            Model = content.Load<Model>("box");
            Position = Vector3.Down;
        }

        public bool isValidPosition(Vector3 posi)
        {
            bool blocked = false;

            foreach (Solid s in game.cspawn.solids)
            {
                foreach (BoundingBox box in s.getBoundingBoxes())
                {
                    if (boundingBox.Intersects(box))
                        blocked = true;
                }
            }
            return (game.terrain.isValidPosition(posi) && !blocked);
        }

        private void checkBoxCollision()
        {
            foreach (MovableBox m in game.cspawn.boxes)
            {
                if (this.boundingBox.Intersects(m.boundingBox))
                {
                    if (this.Position.X < m.Position.X - 1.5 && (m.Position.Z + 1.5 > this.Position.Z || m.Position.Z - 1.5 < this.Position.Z))
                        m.Position = new Vector3(m.Position.X + 1, game.terrain.Height[(int)m.Position.X + 1, (int)-m.Position.Z] + 3, m.Position.Z);
                    else if (this.Position.X > m.Position.X + 1.5 && (m.Position.Z + 1.5 > this.Position.Z || m.Position.Z - 1.5 < this.Position.Z))
                        m.Position = new Vector3(m.Position.X - 1, game.terrain.Height[(int)m.Position.X - 1, (int)-m.Position.Z] + 3, m.Position.Z);

                    else if (this.Position.Z < m.Position.Z && (m.Position.X + 1.5 > this.Position.X || m.Position.X - 1.5 < this.Position.X))
                        m.Position = new Vector3(m.Position.X, game.terrain.Height[(int)m.Position.X, (int)-m.Position.Z + 1] + 3, m.Position.Z + 1);
                    else if (this.Position.Z > m.Position.Z && (m.Position.X + 1.5 > this.Position.X || m.Position.X - 1.5 < this.Position.X))
                        m.Position = new Vector3(m.Position.X, game.terrain.Height[(int)m.Position.X, (int)-m.Position.Z - 1] + 3, m.Position.Z - 1);
                    else
                    {
                        // dont move... cuz runnin agaist da fuqin box --
                    }
                }
            }
        }
        public void UpdateBoundingBox()
        {
         boundingBox = new BoundingBox(new Vector3(Position.X - 1 , Position.Y - 3, Position.Z - 1),new Vector3(Position.X + 1, Position.Y + 3, Position.Z + 1));
       }
        //TODO: called when collision with player (or box?) is spotted.
        public void moveBox()
        {
        }
    }
}
